<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索 -->
      <el-input
        placeholder="请输入内容"
        class="input-with-select"
        v-model="queryInfo.query"
        clearable
        style="width:30%;margin-bottom:15px;"
        @clear="getuserList"
      >
        <el-button
          slot="append"
          type="primary"
          icon="el-icon-search"
          @click="getuserList"
        ></el-button>
      </el-input>

      <!-- 内容列表 -->
      <el-table :data="userList" style="width: 100%" border v-loading="loadingSwitch">
        <el-table-column prop="workerNumber" label="教职工号"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="position.name" label="职位"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column
          prop="rofessional.name"
          label="所属专业"
        ></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              type="primary"
              size="mini"
              @click="$router.push(`/createUser/${scope.row._id}`)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="userDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="total, prev, pager, next"
        :total="total"
        style="margin-top: 20px"
        :current-page="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userList: [],
      queryInfo: {
        query: "",
        pagenum: this.$route.query.pagenum*1 || 1, //第几页
        pagesize: 7, //每页显示个数
      },
      //总条数
      total: 0,
      //加载开关
      loadingSwitch:true
    };
  },
  methods: {
    //获取用户列表
    async getuserList() {
      this.loadingSwitch = true
      const res = await this.$http.get("/user", { params: this.queryInfo });
      if (res.data.status === 200) {
        let time = setTimeout(() => {
          this.userList = res.data.data.model;
          this.queryInfo.pagenum = res.data.data.pagenum;
          this.total = res.data.data.total;
          this.loadingSwitch = false
        },1000)
      }
    },
    //删除用户
    userDel(row) {
      this.$confirm(`是否确定删除 ${row.name}`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$http.delete(`/user/${row._id}`);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getuserList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //页数改变触发事件
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.$router.replace({
        query:{
          pagenum:val
        }
      })
      this.getuserList();
    },
  },
  created() {
    this.getuserList();
  },
};
</script>

<style lang="less" scoped>
</style>